package com.woniu.micropropertyserver.mapper;


import com.woniu.microcommonsserver.entity.OwnerInfoEntity;
import com.woniu.micropropertyserver.util.PageRest;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface OwnerMapper {

    @ResultType(OwnerInfoEntity.class)
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg,app_status appStatus from owner_info where status=0")
    List<OwnerInfoEntity> findall();

    @ResultType(OwnerInfoEntity.class)
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg,app_status appStatus from owner_info where status=0 and name=#{name}")
    List<OwnerInfoEntity> findByName(String deployTeIcName, String name);

    List<OwnerInfoEntity> findAllByParams(@Param("pp") PageRest pageRest,@Param("info")OwnerInfoEntity ownerInfoEntity);

    @ResultType(OwnerInfoEntity.class)
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "houseInfos",column = "card_id",javaType =List.class ,many = @Many(select = "com.woniu.micropropertyserver.housemag.mapper.HouseMapper.findByOid",fetchType = FetchType.LAZY)),
          /*  @Result(property = "parkCarInfos",column = "card_id",javaType =List.class ,many = @Many(select = "com.woniu.micropropertyserver.parkmag.mapper.ParkMapper.findByOCard",fetchType = FetchType.LAZY)),
           *//* @Result(property = "relativesInfos",column = "id",javaType =List.class ,many = @Many(select ="com.woniu.micropropertyserver.mapper.RelativesInfoMapper.findByOwner",fetchType = FetchType.LAZY)),*/
            @Result(property = "renterInfos",column = "id",javaType =List.class ,many = @Many(select = "com.woniu.micropropertyserver.mapper.RenterInfoMapper.findByOwner",fetchType = FetchType.LAZY))
    })
    @Select("select id,name,sex,card_type cardType,card_id cardId,phone,item,house_num houseNum,headimg,app_status appStatus from owner_info where id=#{id}")
    OwnerInfoEntity findById(Long id);

    @Select("select id,name,house_num houseNum from owner_info where id=#{id}")
    OwnerInfoEntity findNameById(Long id);

    @Update("<script>" +
            " update owner_info" +
            "<set>" +
            "<if test='o.name!=null'>" +
            "name=#{o.name}," +
            "</if>" +
            "<if test='o.sex!=null'>" +
            "sex=#{o.sex}," +
            "</if>" +
            "<if test='o.cardType!=null'>" +
            "card_type=#{o.cardType}," +
            "</if>" +
            "<if test='o.cardId!=null'>" +
            "card_Id=#{o.cardId}," +
            "</if>" +
            "<if test='o.houseNum!=null'>" +
            "house_num=#{o.houseNum}," +
            "</if>" +
            "<if test='o.item!=null'>" +
            "item=#{o.item}," +
            "</if>" +
            "<if test='o.headimg!=null'>" +
            "headimg=#{o.headimg}," +
            "</if>" +
            "<if test='o.appStatus!=null '>" +
            "app_status=#{o.appStatus}," +
            "</if>" +
             "</set>where card_id=#{o.cardId}" +
            "</script>")
    int updateInfo(@Param("o") OwnerInfoEntity ownerInfo);

    @Update("update  owner_info set status=1 where id=#{id}")
    int delInfo(Long id);

    @Options(
            useGeneratedKeys = true,
            keyProperty = "o.id"
    )
     @Insert("insert into owner_info(id,name,sex,card_type,card_id,phone,item,house_num,headimg,app_status,status)values(null,#{o.name},#{o.sex},#{o.cardType},#{o.cardId},#{o.phone},#{o.item},#{o.houseNum},#{o.headimg},#{o.appStatus},0 ) ")
    Long saveInfo(@Param("o") OwnerInfoEntity ownerInfo);

    @ResultType(OwnerInfoEntity.class)
    @Select("select id,name,card_type cardType,card_id cardId,phone from owner_info where card_id=#{id}")
    OwnerInfoEntity findByCard(@Param("id")String id);

    //=========================
    /**
     * 根据 房间id，查询它的业主
     * @param sid 房间id
     * @return
     */
    @ResultType(OwnerInfoEntity.class)
    @Select("select o.id,o.name,o.phone " +
            "from owner_info as o,house_info as h where h.fk_user_id=o.id and h.fk_space_id=#{sid}")
    OwnerInfoEntity findOwerBySid(Long sid);

    /**
     * 根据 房间id，查询它的业主id
     * @param sid
     * @return
     */
    @ResultType(Long.class)
    @Select("select fk_user_id from house_info where fk_space_id=#{sid}")
    Long findOwerIdBySid(Long sid);

    /**
     * 根据 业主id，查询业主名字
     * @param id
     * @return
     */
    @ResultType(String.class)
    @Select("select name from owner_info where id=#{id}")
    String findOwerNameById(Long id);

    @ResultType(OwnerInfoEntity.class)
    @Select("select * from owner_info where name =#{name} and house_num=#{houseNum}")
    OwnerInfoEntity findByNameAndHouse(@Param("name") String name,@Param("houseNum") String houseNum);

    @Select("SELECT COUNT(NAME) FROM owner_info")
    Integer countNum();

    @ResultType(OwnerInfoEntity.class)
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "houseInfos",column = "card_id",javaType =List.class ,many = @Many(select = "com.woniu.micropropertyserver.housemag.mapper.HouseMapper.findByOid",fetchType = FetchType.LAZY)),
    })
    @Select("select id,name,card_type cardType,card_id cardId from owner_info where id=#{id}")
    OwnerInfoEntity findHouseById(Long id);
}
